const { list,xiugai,zidianlist,dianya,addeq,deleq } = require("../../utils/api")

// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vDianya:"",
    aa:"",
    eqnum:"",
    eq:{},
    coldh:"",
    coldd:"",
    hoth:"",
    hotd:"",
    neweqname:"",
    newnum:"",
    newdizhi:"",
    win:false,
    show:"",
    onsow:false,
    newdianya:"",
    openPicker: false,//打开组件
    timeRegion:'',//
    list:[
    ]
  },
  startime: function(e) {
   
    this.setData({
      starttime: e.detail.value,
      
    })
  
  },

  offtime: function(e) {
    
    this.setData({
      offtime: e.detail.value,
     
    })
    console.log(this.data.offtime)
  },

  starttime2: function(e) {
   
    this.setData({
      starttime2: e.detail.value
    }) 
    console.log(this.data.starttime2)
  },

  offtime2: function(e) {
   
    this.setData({
      offtime2: e.detail.value
    })
    console.log(this.data.offtime2)
  },
  coldh(e) {
     console.log(e)  
     this.setData({'eq.lengmax':e.detail.value })},
  coldd(e){  this.setData({'eq.lengmin':e.detail.value })},
  hoth(e){  this.setData({'eq.remax':e.detail.value })},
  hotd(e){  this.setData({'eq.remin':e.detail.value })},

  onselect({ detail }) {
    this.setData({
      timeRegion: detail.timeRegion || this.data.timeRegion,
      openPicker: false,
    });
  },

  inputname(e){
    this.setData({
      neweqname:e.detail.value
    })
   
  },
  inputnum(e){
    this.setData({
      newnum:e.detail.value
    })
    
  },
  inputdizhi(e){
    this.setData({
      newdizhi:e.detail.value
    })
   
  },
  newdianya(e){
    this.setData({
      newdianya:e.detail
    })
   
  },
  sear(){
    let that=this
    wx.scanCode({
      success (res) {
        that.setData({
          newnum:res.result
        })
        
      }
    })
  },
  dianya(){
    let that=this
    if(this.data.newdianya.length==0){
      wx.showToast({
        title: '请选择电压',
        icon: 'none',
        duration: 2000
      })
      return
    }
   
    let p={eqVoltage:this.data.newdianya,
       id:this.data.eq.id,
       shezhiip:2
    }
    xiugai(p).then(res=>{
      console.log(res)
      if(res.code==200){
        wx.showToast({
          title: '电压修改成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          dianya:false,
          win:false
        })
        that.onShow()
        console.log(res,'电压修改成功')
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  
  add(){
    this.setData({
      onshow:true,
      win:true
    })
  },
  xiangqing(e){
    
    let me=JSON.stringify(e.currentTarget.dataset.me)
    wx.navigateTo({
      url: '/pages/xiangqing/xiangqing?me='+me,
    })

  },
  shezhi(e){
    console.log(e)
    this.setData({
      show:e.currentTarget.dataset.number
    })
  },
  put(){
    this.setData({
      show:""
    })
  },
  off(){
    this.setData({
      onshow:false,
      win:false,
      dianya:false,
      dianchi:false,
      model:false,
      light:false,
      wenkong:false,
      other:false,
      revise:false

    })
  },
  1(){},
  del(e){
    
     let p={
       id:e.currentTarget.dataset.id
     }
     let that=this
    wx.showModal({
      title: '提示',
      content: '删除此设备',
      success (res) {
        if (res.confirm) {
        deleq(p).then(res=>{
          console.log(res)
          if(res.code==200){
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })
            that.onShow()
          }else{
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  yes(){
    
    if(this.data.neweqname.length==0){
      wx.showToast({
        title: '输入设备名称',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.newnum.length==0){
      wx.showToast({
        title: '输入设备编码',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.newdizhi.length==0){
      wx.showToast({
        title: '输入设备地址',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let that=this
    wx.showModal({
      title: '提示',
      content: '添加此设备',
      success (res) {
        if (res.confirm) {

          let p={
            eqName:that.data.neweqname,
            equipmentNo:that.data.newnum,
            eqAddress:that.data.newdizhi
          }
         addeq(p).then(res=>{
           if(res.code==200){
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 2000
            })
            that.setData({
              onshow:false,
              win:false
            })
            that.onShow()
           }else{
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
           }
           console.log("测试是否添加成功",res)
         })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  close(){},
  dianchi1(e){
   
    if(this.newdianchi.length==0){
      wx.showToast({
        title: '请选择类型',
      })
      return
    }
   let p={
    eqType:this.data.newdianchi,
    id:this.data.eq.id,
    shezhiip:3
    }
    xiugai(p).then(res=>{
      
      if(res.code==200){
console.log(res,'电池修改成功')
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000
        })
  this.setData({
       win:false,
       dianchi:false
     })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
   
  },
  canshu(e){
    wx.navigateTo({
      url: '/pages/parameter/parameter',
    })

  },
  voltage(e){
   this.setData({
     win:true,
     dianya:true,
     eq:e.currentTarget.dataset.item
   })
   console.log(this.data.eq)
   let p={
    dictType:this.data.booklist[23].dictType
   }
   
   dianya(p).then(res=>{
     this.setData({
       vlist:res.data
     })
     
   })
  },
  cell(e){
    this.setData({
      dianchi:true,
      win:true,
      eq:e.currentTarget.dataset.item
    })
    console.log(this.data.eq)
    let p={
      dictType:this.data.booklist[16].dictType
     }
     
     dianya(p).then(res=>{
       this.setData({
         vlist:res.data,
        //  vDianya:res.data[this.data.eq.eqType].dictLabel
       })
       console.log(this.data.vDianya)
       
       
     })
    
  },
  newmodel(e){
    this.setData({
      'eq.modelLoad':e.detail
    })
  },
  model(e){
   
   this.setData({
    model:true,
    win:true,
    eq:e.currentTarget.dataset.item
   })
   console.log(this.data.eq)
   let p={
    dictType:this.data.booklist[14].dictType
   }
   
   dianya(p).then(res=>{
     this.setData({
       vlist:res.data,
       vDianya:res.data[this.data.eq.modelLoad].dictLabel
     })
     console.log(this.data.vDianya)
   })
    
  },
  fuzai(){
     let that=this
     let p={
       id:this.data.eq.id,
       modelLoad:this.data.eq.modelLoad,
       shezhiip:4
     }
     xiugai(p).then(res=>{
       if(res.code==200){
       console.log(res,'模式修改成功')
       wx.showToast({
        title: '模式修改成功',
        icon: 'success',
        duration: 2000
      })
       this.setData({
        model:false,
        win:false,
      })
       }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
     })
   
  },
  light(e){
    console.log(e)
    this.setData({
      light:true,
      win:true,
      eq:e.currentTarget.dataset.item,
      // starttime:e.currentTarget.dataset.item.starttime1
      starttime:e.currentTarget.dataset.item.starttime1.substr(11,5),
      offtime:e.currentTarget.dataset.item.endtime1.substr(11,5),
      starttime2:e.currentTarget.dataset.item.starttime2.substr(11,5),
      offtime2:e.currentTarget.dataset.item.endtime2.substr(11,5)
    })
  
      console.log(this.data.eq)
  },
  oflight(e){
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y =date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
    console.log(Y + '年'  + M+ '月' + D+ '日' ); 
    let p={
      shezhiip:5,
      id:this.data.eq.id,
      starttime1:Y + '-'  + M+ '-' + D+" " +this.data.starttime+":00",
      endtime1:Y + '-'  + M+ '-' + D +" "+this.data.offtime+":00",
      starttime2:Y + '-'  + M+ '-' + D+" " +this.data.starttime2+":00",
      endtime2:Y + '-'  + M+ '-' + D +" " +this.data.offtime2+":00"
    }
    xiugai(p).then(res=>{
     if(res.code==200){
       console.log(res,'时间修改成功')
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 2000
      })

      this.setData({
        light:false,
        win:false
      })
     }else{
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
    }
    })
    
  
  },
  valve(e){
   
    this.setData({
      wenkong:true,
      win:true,
      eq:e.currentTarget.dataset.item
    })
 
    
  },
  ofwenkong(e){
   
    if(this.data.eq.lengmax.length==0){
      wx.showToast({
        title: '制冷最高温度',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.eq.lengmin.length==0){
      wx.showToast({
        title: '制冷最低温度',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.eq.remax.length==0){
      wx.showToast({
        title: '加热最高温度',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.eq.remin.length==0){
      wx.showToast({
        title: '加热最低温度',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let p={
      lengmax:this.data.eq.lengmax,
      lengmin:this.data.eq.lengmin,
      remax:this.data.eq.remax,
      remin:this.data.eq.remin,
      id:this.data.eq.id,
      shezhiip:6
    }
    xiugai(p).then(res=>{
      
      if(res.code==200){
        console.log(res,'温度修改成功')
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          wenkong:false,
          win:false
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })

   
  },
  other(e){
   this.setData({
     other:true,
     win:true,
     eq:e.currentTarget.dataset.item
   })
  },
  ofother(e){
    let that=this
    let p={
      id:this.data.eq.id,
      wxcddkstatus:this.data.eq.wxcddkstatus,
      usbdkstatus:this.data.eq.usbdkstatus,
      lyyxdkstatus:this.data.eq.lyyxdkstatus,
      dg1status:this.data.eq.dg1status,
      dg2status:this.data.eq.dg2status,
      jrstatus:this.data.eq.jrstatus,
      zlstatus:this.data.eq.zlstatus,
      shezhiip:7
    }

   
    xiugai(p).then(res=>{
      console.log(res)
      if(res.code==200){
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          other:false,
          win:false
        })
that.onShow()
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
   
  },
  revise(e){
    
    this.setData({
      revise:true,
      win:true,
      eq:e.currentTarget.dataset.item
    })
  },
  ofrevise(e){
    let that=this
    if(this.data.eq.eqName.length==0){
      wx.showToast({
        title: '输入设备名称',
        icon: 'none',
        duration: 2000
      })
      return
    }
 
    if(this.data.eq.eqAddress.length==0){
      wx.showToast({
        title: '输入设备地址',
        icon: 'none',
        duration: 2000
      })
      return
    }
    

    let p={
      eqName:this.data.eq.eqName,
      equipmentNo:this.data.eq.equipmentNo,
      eqAddress:this.data.eq.eqAddress,
       id:this.data.eq.id
    }
    xiugai(p).then(res=>{
      console.log(res)
     if(res.code==200){
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        revise:false,
        win:false
      })
      that.onShow()
     }else{
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
    }
    })
  },
  putname(e){
    this.setData({
      'eq.eqName':e.detail.value
    })
  },
  putnum(e){
    this.setData({
      'eq.equipmentNo':e.detail.value
    })
  },
  putdizhi(e){
    this.setData({
      'eq.eqAddress':e.detail.value
    })
  },
  
  wuxianChange(e){
    if(e.detail.value){
      this.setData({
      'eq.wxcddkstatus':1
      })
    }else(
      this.setData({
        'eq.wxcddkstatus':0
      })
    )
   console.log(this.data.eq.wxcddkstatus)
   
  },
  usbChange(e){
   
    if(e.detail.value){
      this.setData({
        'eq.usbdkstatus':1
      })
    }else(
      this.setData({
        'eq.usbdkstatus':0
      })
    )
   console.log(this.data.eq.usbdkstatus)
  },
  blueChange(e){
    
    if(e.detail.value){
      this.setData({
       'eq.lyyxdkstatus':1
      })
    }else(
      this.setData({
        'eq.lyyxdkstatus':0
      })
    )
   console.log(this.data.eq.lyyxdkstatus)
  },
  deng1Change(e){
    if(e.detail.value){
      this.setData({
        'eq.dg1status':1
      })
    }else(
      this.setData({
        'eq.dg1status':0
      })
    )
   console.log(this.data.eq.dg1status)
  },
  deng2Change(e){
    
    if(e.detail.value){
      this.setData({
        'eq.dg2status':1
      })
    }else(
      this.setData({
        'eq.dg2status':0
      })
    )
   console.log(this.data.eq.dg2status)
  },
  hotChange(e){
    if(e.detail.value){
      this.setData({
       'eq.jrstatus':1
      })
    }else(
      this.setData({
        'eq.jrstatus':0
      })
    )
   console.log(this.data.eq.jrstatus)
  },
  coldChange(e){
    if(e.detail.value){
      this.setData({
        'eq.zlstatus':1
      })
    }else(
      this.setData({
        'eq.zlstatus':0
      })
    )
   console.log(this.data.eq.zlstatus)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
     let p={
      dealerId:wx.getStorageSync('userId')
     }
    list(p).then(res=>{
      console.log(res)
      this.setData({
        list:res.rows
      })
      console.log('获取设备列表',this.data.list)
    })
    zidianlist().then(res=>{
      
      this.setData({
       booklist:res.data
      })
     
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  newdianchi(e){
    
    this.setData({
      newdianchi:e.detail,
      
    })
    
  },
})